﻿package com.thomasbrault.display 
{
	import flash.display.FrameLabel;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;	

	/**
	 * @author thomas
	 */
	public class AbstractButton extends AbstractMovieClip 
	{
		public var zone : Sprite;
		
		override protected function _addedToStage () : void 
		{
			enable();
		}
		
		
		/**
		 * enable button
		 */
		public function enable (  ) : void
		{

			if ( buttonMode ) return;
			addEventListener ( MouseEvent.ROLL_OVER, _onMouse);
			addEventListener ( MouseEvent.ROLL_OUT, _onMouse);
			addEventListener ( MouseEvent.CLICK, _onMouse);
			buttonMode=true;
			mouseEnabled=true;
			mouseChildren=true;
			
		}
		
		
		/**
		 * disable button
		 */
		public function disable (  ) : void
		{
			trace(this,'disable')
			removeEventListener ( MouseEvent.ROLL_OVER, _onMouse );
			removeEventListener ( MouseEvent.ROLL_OUT, _onMouse );
			removeEventListener ( MouseEvent.CLICK, _onMouse );
			buttonMode=false;
			mouseEnabled=false;
			mouseChildren=false;
			
		}
		
		/**
		*
		*/
		override protected function _removedFromStage ( ) : void
		{
			disable();
		}
		
		
		/**
		 * 
		 */
		public function draw ( mc : Sprite = null ) : void
		{
			if ( zone == null){
				zone = new Sprite;
				addChild(zone);
			}
			
			var w : Number;
			var h : Number;
			if ( mc!=null){
				w = mc.width;
				h = mc.height;
			}else{
				w = width;
				h = height;
			}
			
			zone.graphics.clear();
			zone.graphics.beginFill( 0x000000 , 0);
			zone.graphics.drawRect( 0, 0, w, h );
			zone.graphics.endFill();
		}
		
		/**
		 * 
		 */
		protected function _onMouse(event : MouseEvent) : void
		{
			
			switch ( event.type )
			{
				case MouseEvent.ROLL_OVER:
					_over();
				break;
				case MouseEvent.ROLL_OUT:
					_out();
				break;
				case MouseEvent.CLICK:
					_click();
				break;
			}
			
		}
		
		
		/**
		 * can be overrided 
		 */
		protected function _out() : void
		{
			
			var a : Array = currentLabels;
			for each( var labels : FrameLabel in a ) 
			{
				if( labels.name == '_out' ){ 
					gotoAndPlay('_out');
					return;
				}
			}
			
			playToward();
			
		}


		/**
		 * can be overrided 
		 */
		protected function _over() : void
		{
			var a : Array = currentLabels;
			for each( var labels : FrameLabel in a ) 
			{
				if( labels.name == '_over' ){ 
					gotoAndPlay('_over');
					return;
				}
			}
		}
		
		protected function _click ( ) : void
		{
			
		}
	}
}
